{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a5256a7c-6807-41f0-89fd-b2643b36b8bd",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 35.直流电机风扇模块实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：36.直流电机风扇模块实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：直流电机风扇模块实验\n",
    "直流电机风扇模块实验，通过GPIOzero 自带的Motor 直流电机驱动库，可以方便控制直流电机的速度、方向等！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab179391-9c46-48fd-92a4-d95e587c591a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gpiozero import Motor   # 直流电机驱动库\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.定义直流电机风扇驱动管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "Makerobo_motor = Motor(forward=17, backward=18, enable=19)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ecf06dd-3b8e-4925-8b8a-6b4facc7a032",
   "metadata": {},
   "source": [
    "## 3.初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d51bb77a-312f-4bf3-8d02-b892ec11c96b",
   "metadata": {},
   "outputs": [],
   "source": [
    "fs_directions = {'clockwiseOpen': Makerobo_motor.forward,'anticlockwiseOpen':Makerobo_motor.backward,'STOP': Makerobo_motor.stop} # 定义开和关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 4.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\t\n",
    "def makerobo_loop():\n",
    "    while True:\n",
    "        for action in ['clockwiseOpen', 'STOP', 'anticlockwiseOpen', 'STOP']:  # 循环通过定义的动作来控制电机方向\n",
    "            fs_directions[action]()  # 执行当前动作\n",
    "            print(f\"{action}\")       # 打印提示信息\n",
    "            sleep(5)                 # 延时5S，开启下一个动作\n",
    "\n",
    "# 释放资源\n",
    "def destroy():\n",
    "\t# 关闭风扇\n",
    "    Makerobo_motor.stop()\n",
    "    \n",
    "    \n",
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()        #  调用循环函数\n",
    "\texcept KeyboardInterrupt:  #  当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tdestroy()   # 关闭风扇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bdbd5f5-39d3-4b9d-aade-dc787797fec9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
